package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopCommodity;
import com.woniuxy.common.entity.ShopMerchantRepository;
import com.woniuxy.common.entity.ShopMerchantRepositoryExample;
import com.woniuxy.common.mapper.ShopMerchantRepositoryMapper;
import com.woniuxy.joylifeshop.exception.MerchantRepositoryException;
import com.woniuxy.joylifeshop.service.MerchantRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class MerchantRepositoryServiceImpl implements MerchantRepositoryService {

    @Autowired
    ShopMerchantRepositoryMapper shopMerchantRepositoryMapper;
    /**
    * @Author SooYoo
    * @Description 根据条件查询仓储对象
    * @Param
    * @Return
    **/
    @Override
    public List<ShopMerchantRepository> readMerchantRepositoryByCondition(ShopMerchantRepository shopMerchantRepository) {

        try {
            ShopMerchantRepositoryExample shopMerchantRepositoryExample = new ShopMerchantRepositoryExample();
            ShopMerchantRepositoryExample.Criteria criteria = shopMerchantRepositoryExample.createCriteria();
            criteria.andMerchantIdEqualTo(shopMerchantRepository.getMerchantId());

            if (shopMerchantRepository.getRepositoryId()!=null && !shopMerchantRepository.getRepositoryId().equals("")) {
                criteria.andRepositoryIdEqualTo(shopMerchantRepository.getRepositoryId());
            }

            if (shopMerchantRepository.getCommodityId()!=null && !shopMerchantRepository.getCommodityId().equals("")) {
                criteria.andCommodityIdEqualTo(shopMerchantRepository.getCommodityId());
            }

            List<ShopMerchantRepository> shopMerchantRepositories = shopMerchantRepositoryMapper.selectByExample(shopMerchantRepositoryExample);

            return  shopMerchantRepositories;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantRepositoryException();
        }
    }

    /**
    * @Author SooYoo
    * @Description 删除仓储对象
    * @Param
    * @Return
    **/
    @Override
    public void deleteRepositoryByRepositoryId(String information) {

        try {
            shopMerchantRepositoryMapper.deleteByPrimaryKey(information);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantRepositoryException();
        }

    }
    /**
    * @Author SooYoo
    * @Description 调拨方法
    * @Param
    * @Return
    **/
    @Override
    public void allotRepository(ShopMerchantRepository shopMerchantRepository) {

        try {
            //找到原对象
            ShopMerchantRepository shopMerchantRepositoryReality = shopMerchantRepositoryMapper.selectByPrimaryKey(shopMerchantRepository.getRepositoryId());
//            System.out.println(shopMerchantRepository);
//            System.out.println(shopMerchantRepositoryReality);
            //给原对象加仓储
            shopMerchantRepositoryReality.setStock(shopMerchantRepositoryReality.getStock()+shopMerchantRepository.getStock());
            //更新
            shopMerchantRepositoryMapper.updateByPrimaryKey(shopMerchantRepositoryReality);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantRepositoryException();
        }

    }

    /**
    * @Author SooYoo
    * @Description 新增一个仓储对象
    * @Param
    * @Return
    **/
    @Override
    public void addRepositoryObject(ShopCommodity shopCommodity) {


        ShopMerchantRepository shopMerchantRepository = new ShopMerchantRepository();

        shopMerchantRepository.setRepositoryId(UUID.randomUUID().toString());
        shopMerchantRepository.setCommodityId(shopCommodity.getShopId());
        shopMerchantRepository.setMerchantId(shopCommodity.getMerchantId());
        shopMerchantRepository.setStock(0L);


        shopMerchantRepositoryMapper.insert(shopMerchantRepository);

    }

    @Override
    public ShopMerchantRepository cheakRepository(String comId) {


        ShopMerchantRepositoryExample shopMerchantRepositoryExample = new ShopMerchantRepositoryExample();
        ShopMerchantRepositoryExample.Criteria criteria = shopMerchantRepositoryExample.createCriteria();
        criteria.andCommodityIdEqualTo(comId);

        List<ShopMerchantRepository> shopMerchantRepositories = shopMerchantRepositoryMapper.selectByExample(shopMerchantRepositoryExample);


        return shopMerchantRepositories.get(0);

    }
}
